﻿using LuaInterface;
using UnityEngine;
using UnityEngine.UI;

namespace Framework.UGUI
{
    public class ScrollRectWrap : MonoBehaviour
    {
        public static ScrollRectWrap Get(GameObject go)
        {
            ScrollRect scrollRect = go.GetComponent<ScrollRect>();
            if (scrollRect != null)
            {
                return go.OnceAddComponent<ScrollRectWrap>();
            }
            return null;
        }

        public static ScrollRectWrap GetWithPath(GameObject go, string path)
        {
            var scrollRectTransform = go.transform.Find(path);
            if (scrollRectTransform != null)
            {
                return Get(scrollRectTransform.gameObject);
            }
            return null;
        }

        private ScrollRect _scrollRect;
        [NoToLua]
        public ScrollRect scrollRect
        {
            get
            {
                if (_scrollRect == null)
                {
                    _scrollRect = gameObject.GetComponent<ScrollRect>();
                }
                return _scrollRect;
            }
        }

        public RectTransform content => scrollRect.content;
        public float verticalNormalizedPosition
        {
            get => scrollRect.verticalNormalizedPosition;
            set => scrollRect.verticalNormalizedPosition = value;
        }
        public float horizontalNormalizedPosition
        {
            get => scrollRect.horizontalNormalizedPosition;
            set => scrollRect.horizontalNormalizedPosition = value;
        }
        public Vector2 normalizedPosition
        {
            get => scrollRect.normalizedPosition;
            set => scrollRect.normalizedPosition = value;
        }
        public int movementType
        {
            get => (int)scrollRect.movementType;
            set => scrollRect.movementType = (ScrollRect.MovementType)value;
        }

        private GeneralCallBack _onValueChangedCallback;

        public void AddOnValueChanged(LuaFunction onValueChanged, LuaTable onValueChangedTarget)
        {
            GeneralCallBack.GetOrReUse(ref _onValueChangedCallback, onValueChanged, onValueChangedTarget);
            scrollRect.onValueChanged.RemoveListener(OnValueChanged);
            scrollRect.onValueChanged.AddListener(OnValueChanged);
        }

        public void RemoveOnValueChanged()
        {
            _clearCallBack();
            scrollRect.onValueChanged.RemoveListener(OnValueChanged);
        }

        private void OnValueChanged(Vector2 normalizedPos)
        {
            if (_onValueChangedCallback != null)
            {
                GeneralCallBack.Invoke(ref _onValueChangedCallback, normalizedPos.x, normalizedPos.y);
            }
        }

        private void _clearCallBack()
        {
            GeneralCallBack.RecycleInst(ref _onValueChangedCallback);
        }

        void OnDestroy()
        {
            if (_onValueChangedCallback != null)
            {
                UIEventDebugger.LogUIEventError("ScrollRectWrap", gameObject);
            }
            _clearCallBack();
        }
    }
}
